import 'package:bilibili/basic/util/ui_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class LoginInput extends StatefulWidget {
  final String title;
  final String hint;
  final ValueChanged<String> onChanged;
  final ValueChanged<bool>? focusChanged;
  final bool lineStretch;
  final bool? obscureText;
  final TextInputType? keyboardType;
  @override
  _LoginInputState createState() => _LoginInputState();

  const LoginInput({Key? key, required this.title, required this.hint, required this.onChanged, this.focusChanged,
    this.lineStretch = true, this.obscureText = false, this.keyboardType = TextInputType.text}) : super(key: key);
}

class _LoginInputState extends State<LoginInput> {
  final _focusNode = FocusNode();

  @override
  void initState() {
    _focusNode.addListener(() {
      if (widget.focusChanged != null) {
        widget.focusChanged!(_focusNode.hasFocus);
      }
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Row(
            children: [
              Container(
                padding: EdgeInsets.only(left: 15),
                child: Text(
                  widget.title,
                  style: TextStyle(fontSize: 16),
                ),
              ),
              _input(),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(left: !widget.lineStretch ? 15 : 0, right: !widget.lineStretch ? 15 : 0),
            child: Divider(height: 1, thickness: 1,color: Colors.red,),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  Widget _input() {
    return Expanded(
      child: TextField(
        focusNode: _focusNode,
        onChanged: widget.onChanged,
        obscureText: widget.obscureText!,
        keyboardType: widget.keyboardType,
        autofocus: true,
        cursorColor: primary,
        style: TextStyle(fontSize: 16, color: Colors.black, fontWeight: FontWeight.w300),
        decoration: InputDecoration(
          contentPadding: EdgeInsets.only(left: 20, right: 20),
          border: InputBorder.none,
          hintText: widget.hint,
          hintStyle: TextStyle(fontSize: 15,color: Colors.grey)
        ),
      ),
    );
  }
}
